package com.wodogs.utils.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WriteLog {
	public static final String FILE_PATH = "D:/mercury";
	public static final String sdf_pattern = "yyyy-MM-dd HH:mm:ss";
	public static final String ymd_pattern = "yyyy-MM-dd";

	public static void main(String[] args) {

		File file = new File("c:/newfile.txt");
		String content = "This is the text content";

		try (FileOutputStream fop = new FileOutputStream(file)) {

			// if file doesn't exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}

			// get the content in bytes
			byte[] contentInBytes = content.getBytes();

			fop.write(contentInBytes);
			fop.flush();
			fop.close();

			System.out.println("Done");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void write(String content){
		SimpleDateFormat SDF_TIME = new SimpleDateFormat(sdf_pattern);
		SimpleDateFormat SDF = new SimpleDateFormat(ymd_pattern);
		String time = SDF_TIME.format(new Date());
		File path = new File(FILE_PATH);
		if(!path.exists()){
			path.mkdirs();
		}
		File file = new File(FILE_PATH+"/"+SDF.format(new Date())+".log");
		try (FileOutputStream fop = new FileOutputStream(file,true)) {

			if (!file.exists()) {
				file.createNewFile();
			}

			// get the content in bytes
			byte[] contentInBytes = (time+"  "+content+"\n").getBytes();

			fop.write(contentInBytes);
			fop.flush();
			fop.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
